/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action.phonenumber;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import apus.action.Action;
import apus.action.ActionResult;
import apus.entity.Organization;
import apus.entity.Person;
import apus.entity.PhoneNumber;
import apus.persistence.OrganizationDAO;
import apus.persistence.PersonDAO;
import apus.persistence.PhoneNumberDAO;
import apus.services.ServiceLocator;

/**
 *
 * @author Admin
 */
public class PhoneNumberEditAction implements Action {

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        if (request.getParameter("id") != null) {
            int id = Integer.parseInt(request.getParameter("id"));
            PhoneNumber phoneNumber = ServiceLocator.getService(PhoneNumberDAO.class).read(id);
            List<Person> perList=ServiceLocator.getService(PersonDAO.class).findAll();
            List<Organization> orgList=ServiceLocator.getService(OrganizationDAO.class).findAll();
            request.setAttribute("phoneNumber", phoneNumber);
            request.setAttribute("perList", perList);
            request.setAttribute("orgList", orgList);
            return ActionResult.forward("/WEB-INF/View/PhoneNumberView/EditPhoneNumberView.jsp");
        } else {
            List<PhoneNumber> phoneNumberList = ServiceLocator.getService(PhoneNumberDAO.class).findAll();
            request.setAttribute("phoneNumberList", phoneNumberList);
            return ActionResult.redirect("phoneNumber.list");
        }
    }

    @Override
    public String getName() {
        return "phoneNumber.edit";
    }

}